################################################################################
# Legislature Size and Party Unity: Supplementary Materials   ##################
# Jaclyn Kaslovsky and Michael Olson ###########################################
################################################################################

################################################################################
# working directory, packages and functions ####################################
################################################################################


#Set a working directory to output all results. 
#setwd("~/Replication Package/")
#Put data in a subfolder called "Data". 
#Make an empty subfolder called "Paper" that contains another subfolder called "appendix_materials".

#load packages
require(lfe)
require(stargazer)
require(ggplot2)
require(readxl)
require(dplyr)
require(starpolishr)
require(tidyr)
require(english)
require(haven)

################################################################################  
# Load in the Data  ############################################################
################################################################################

dat_ilin <- read.csv("./Data/ilin_data.csv")

dat_njvt <- read.csv("./Data/njvt_data.csv")

################################################################################
#Section A Descriptive Information
################################################################################

# Table A.1 Illinois

ilsumstats <- dat_ilin[dat_ilin$state=="IL",]
ilsumstats <- ilsumstats %>% select("partyunity","partyunityvotes","bigger","majority","maj_share", "inc")
stargazer(ilsumstats,
          summary.stat = c("mean","sd","min","median","max","n"),
          covariate.labels = c("Party Unity","No. of Party Unity Votes","Chamber Larger","In Majority","Majority Seat Share","Incumbent"),
          out="./Paper/appendix_materials/ilsumstats.tex",digits=3,digits.extra=3,
          title="Summary Statistics: Illinois, 1838 to 1851  \\vspace{-0.75em}",
          label="ilsumstats")

# Table A.2 Indiana

insumstats <- dat_ilin[dat_ilin$state=="IN",]
insumstats <- insumstats %>% select("partyunity","partyunityvotes","bigger","majority","maj_share", "inc")
stargazer(insumstats,
          summary.stat = c("mean","sd","min","median","max","n"),
          covariate.labels = c("Party Unity","No. of Party Unity Votes","Chamber Larger","In Majority","Majority Seat Share","Incumbent"),
          out="./Paper/appendix_materials/insumstats.tex",digits=3,digits.extra=3,
          title="Summary Statistics: Indiana, 1838 to 1851  \\vspace{-0.75em}",
          label="insumstats")

# Table A.3 New Jersey

njsumstats <- dat_njvt[dat_njvt$state=="NJ",]
njsumstats <- njsumstats %>% select("partyunity","partyunityvotes","bigger","majority","maj_share", "inc")
stargazer(njsumstats,
          summary.stat = c("mean","sd","min","median","max","n"),
          covariate.labels = c("Party Unity","No. of Party Unity Votes","Chamber Larger","In Majority","Majority Seat Share", "Incumbent"),
          out="./Paper/appendix_materials/njsumstats.tex",digits=3,digits.extra=3,
          title="Summary Statistics: New Jersey, 1958-1974 \\vspace{-0.75em}",
          label="njsumstats")

# Table A.4 Vermont

vtsumstats <- dat_njvt[dat_njvt$state=="VT",]
vtsumstats <- vtsumstats %>% select("partyunity","partyunityvotes","bigger","majority","maj_share", "inc")

stargazer(vtsumstats,
          summary.stat = c("mean","sd","min","median","max","n"),
          covariate.labels = c("Party Unity","No. of Party Unity Votes","Chamber Larger","In Majority","Majority Seat Share", "Incumbent"),
          out="./Paper/appendix_materials/vtsumstats.tex",digits=3,digits.extra=3,
          title="Summary Statistics: Vermont, 1957-1973 \\vspace{-0.75em}",
          label="vtsumstats")

# Figure A. 1 legislators before and after and across size changes, by state ###############

ilsizevariation <- aggregate(bigger~ko_id,FUN=mean,data=dat_ilin[dat_ilin$state=="IL" & !is.na(dat_ilin$partyunity),])
njsizevariation <- aggregate(bigger~id,FUN=mean,data=dat_njvt[dat_njvt$state=="NJ" & !is.na(dat_njvt$partyunity),])
vtsizevariation <- aggregate(bigger~id,FUN=mean,data=dat_njvt[dat_njvt$state=="VT" & !is.na(dat_njvt$partyunity),])

tab <- as.data.frame(rbind(c(sum(ilsizevariation$bigger==1),sum(ilsizevariation$bigger==0),sum(ilsizevariation$bigger<1 & ilsizevariation$bigger>0)),
                           c(sum(njsizevariation$bigger==1),sum(njsizevariation$bigger==0),sum(njsizevariation$bigger<1 & njsizevariation$bigger>0)),
                           c(sum(vtsizevariation$bigger==1),sum(vtsizevariation$bigger==0),sum(vtsizevariation$bigger<1 & vtsizevariation$bigger>0))))

colnames(tab) <- c("Exclusively Bigger","Exclusively Smaller","Both Bigger and Smaller")
tab$State <- c("Illinois","New Jersey","Vermont")
tab <- tab[,c("State","Exclusively Bigger","Exclusively Smaller","Both Bigger and Smaller")]

tab_long <- tab %>% pivot_longer(cols=`Exclusively Bigger`:`Both Bigger and Smaller`)
tab_long$name <- factor(tab_long$name,levels=c("Exclusively Smaller","Exclusively Bigger","Both Bigger and Smaller"))

ggplot(data=tab_long,aes(x=State,y=value,group=name,colour=name,fill=name))+
  geom_bar(stat="identity")+
  scale_fill_grey(end=0.7)+scale_colour_grey(end=0.7)+
  theme_minimal()+
  ylab("")+
  labs(fill="Number of legislators serving when chamber is...",
       colour="Number of legislators serving when chamber is...")+
  theme(legend.position="bottom")+
  guides(fill=guide_legend(nrow=3,byrow=TRUE))

ggsave("./Paper/appendix_materials/n_legs.pdf",device="pdf",width=7,height=7,units="in")

# Figure A. 2 number of party unity votes  ##############

dat_puv <- dat_ilin[,c("year","state","all_votes","tot_puvotes")] %>% distinct(.keep_all=T)  
njvt_puv <- dat_njvt[,c("year","state","all_votes","tot_puvotes")] %>% distinct(.keep_all=T)  

puv <- rbind(dat_puv,njvt_puv)
puv$prop_puv <- puv$tot_puvotes/puv$all_votes

ggplot(data=puv,aes(x=year,y=tot_puvotes))+
  geom_point()+geom_line()+
  facet_wrap(~state,nrow=2,scales="free")+
  theme_bw()+theme(strip.background=element_rect(fill="white"))+
  xlab("Year")+ylab("Party Unity Votes")

ggsave("./Paper/appendix_materials/pu_votes.pdf",width=7,height=4)

# Figure A.3 proportion of party unity votes  ##############

ggplot(data=puv,aes(x=year,y=prop_puv))+
  geom_point()+geom_line()+
  facet_wrap(~state,nrow=2,scales="free")+
  theme_bw()+theme(strip.background=element_rect(fill="white"))+
  xlab("Year")+ylab("Party Unity Votes / All Votes")

ggsave("./Paper/appendix_materials/pu_prop.pdf",width=7,height=4)

################################################################################
# Section A.3 Exploring Other Outcomes in the Illinois House 
################################################################################

# Figure A.4 laws and resolutions ##############################################

my_data <- as.data.frame(read_excel("Data/Law_IL.xlsx", sheet="Laws"))
my_data$Type <- "Law"

my_res <- as.data.frame(read_excel("Data/Law_IL.xlsx", sheet="Joint Resolutions"))
my_res$Type <- "Joint Resolution"
my_res <- plyr::rename(my_res,replace=c("JointResolution"="Law","...5"="Session"))

my_data <- rbind(my_data[,c("Year","Session","Law","Type")],my_res[,c("Year","Session","Law","Type")])

my_data <- my_data[my_data$Year!=1839 & my_data$Year!=1852,]
my_data <- my_data[is.na(my_data$Session) | my_data$Session=="Session 1",]

dat_collapsed <- my_data %>%
  group_by(Year, Type)%>%
  summarise(num_laws = sum(Law))

dat_collapsed$bigger3 <- ifelse(dat_collapsed$Year<1842,1,ifelse(dat_collapsed$Year<=1846,2,3))

ggplot(data=dat_collapsed,aes(x=Year,y=num_laws,group=interaction(bigger3,Type),colour=Type))+
  stat_summary(geom = "point", fun="mean",size=2.5)+
  stat_summary(geom = "line", fun="mean",size=1)+
  xlab("Year")+ylab("Number of Laws Passed")+
  geom_vline(xintercept=1841,colour="indianred",linetype=2,size=1.25)+
  geom_vline(xintercept=1847,colour="indianred",linetype=2,size=1.25)+
  theme_minimal()+
  scale_colour_grey(end=0.7)+
  theme(legend.position = "bottom")

ggsave("./Paper/appendix_materials/il_laws.pdf",device="pdf",width=7,height=4,units="in")

# Figure A.5 Committees ##################################################################

my_comms <- as.data.frame(read_excel("Data/Law_IL.xlsx", sheet="Standing Committees"))
my_comms <- my_comms[!is.na(my_comms$Committees),]

my_comms$bigger3 <- ifelse(my_comms$Year<1842,1,ifelse(my_comms$Year<=1846,2,3))

ggplot(data=my_comms,aes(x=Year,y=Committees,group=bigger3))+
  stat_summary(geom = "point", fun="mean",size=2.5)+
  stat_summary(geom = "line", fun="mean",size=1)+
  xlab("Year")+ylab("Number of Standing Committees")+
  geom_vline(xintercept=1841,colour="indianred",linetype=2,size=1.25)+
  geom_vline(xintercept=1847,colour="indianred",linetype=2,size=1.25)+
  scale_colour_viridis_d(begin=0.3,end=0.7)+
  theme_minimal()

ggsave("./Paper/appendix_materials/il_committees.pdf",device="pdf",width=7,height=4,units="in")


################################################################################  
# Robustness Checks              ###############################################
################################################################################

# Table B.1, only SMDs Illinois/Indiana ########################################

dat_ilin$measure <- dat_ilin$bigger
state_smd <- felm(partyunity~measure+majority+maj_share+inc|state+mod_year|0|ko_id,data=dat_ilin[dat_ilin$mmd==0,])
state_int_smd <- felm(partyunity~measure*majority+maj_share+inc|state+mod_year|0|ko_id,data=dat_ilin[dat_ilin$mmd==0,])

dat_ilin$measure <- dat_ilin$lht
state_cont_smd <- felm(partyunity~measure+majority+maj_share+inc|state+mod_year|0|ko_id,data=dat_ilin[dat_ilin$mmd==0,])
state_int_cont_smd <- felm(partyunity~measure*majority+maj_share+inc|state+mod_year|0|ko_id,data=dat_ilin[dat_ilin$mmd==0,])

dist_smd <- felm(partyunity~bigger+majority+maj_share+inc|county+mod_year|0|ko_id,data=dat_ilin[dat_ilin$mmd==0,])
dist_cont_smd <- felm(partyunity~lht+majority+maj_share+inc|county+mod_year|0|ko_id,data=dat_ilin[dat_ilin$mmd==0,])
single_smd <- felm(partyunity~bigger+majority+maj_share+inc|state+mod_year|0|ko_id,data=dat_ilin[dat_ilin$single_il_county_sample==1 & dat_ilin$mmd==0,])
single_cont_smd <- felm(partyunity~lht+majority+maj_share+inc|state+mod_year|0|ko_id,data=dat_ilin[dat_ilin$single_il_county_sample==1 & dat_ilin$mmd==0,])

ilinsg_smd <-  stargazer(state_smd,state_int_smd, state_cont_smd,state_int_cont_smd,
                         dep.var.labels = c("Party Unity Score"),
                         order=c(1,5,2,3,4),
                         covariate.labels = c("Chamber Measure",
                                              "Chamber $\\times$ In Maj.",
                                              "In Majority",
                                              "Majority Seat Share", "Incumbent"),
                         keep.stat = c("n"),report = "vc*s",table.placement = "!ht",
                         add.lines = list(c("Chamber Measure","\\multicolumn{2}{c}{Indicator}","\\multicolumn{2}{c}{Continuous} \\\\ %"),
                                          c("Fixed Effects","\\multicolumn{2}{c}{State + Year}","\\multicolumn{2}{c}{State + Year} \\\\ %")),
                         title = "Illinois and Indiana, 1838 to 1851, Single-Member Districts \\vspace{-0.75em}",label="ilintab_smd",
                         notes.append = FALSE,notes.label = "", no.space=T,
                         digits=3,digits.extra=3,
                         omit.table.layout = "l#",
                         star.char=c("*","**"),star.cutoffs = c(.1,.05),
                         notes="\\parbox[t]{0.75\\textwidth}{\\footnotesize \\textit{Note}: Entries are linear regression coefficients with 
                       standard errors clustered by legislator in parentheses. Observations are at the legislator-biennium level. 
              $^{**}$p$<$0.05, $^{*}$p$<$0.10 (two-tailed).}")   

ilinsg_mech_smd <-  stargazer(dist_smd,dist_cont_smd,single_smd,single_cont_smd,
                              dep.var.labels = c("Party Unity Score"),
                              order=c(1,2,3,4,5),
                              covariate.labels = c("Chamber Larger",
                                                   "Chamber Size",
                                                   "In Majority",
                                                   "Majority Seat Share", "Incumbent"),
                              keep.stat = c("n"),report = "vc*s",table.placement = "!ht",
                              add.lines = list(c("Sample","\\multicolumn{2}{c}{All SMDs}","\\multicolumn{2}{c}{Single IL County + SMD} \\\\ %"),
                                               c("Fixed Effects","\\multicolumn{2}{c}{District + Year}","\\multicolumn{2}{c}{State + Year} \\\\ %")),
                              title = "Illinois and Indiana, 1838 to 1851, Single-Member Districts \\vspace{-0.75em}",label="ilintab_smd",
                              notes.append = FALSE,notes.label = "", no.space=T,
                              digits=3,digits.extra=3,
                              omit.table.layout = "l#",
                              star.char=c("*","**"),star.cutoffs = c(.1,.05),
                              notes="\\parbox[t]{0.75\\textwidth}{\\footnotesize \\textit{Note}: Entries are linear regression coefficients with 
                       standard errors clustered by legislator in parentheses. Observations are at the legislator-biennium level. 
              $^{**}$p$<$0.05, $^{*}$p$<$0.10 (two-tailed).}") 

smd_panels <- star_panel(ilinsg_smd,ilinsg_mech_smd,panel.label.fontface="bold",
                         panel.names = c("Base Models", 
                                         "Mechanism Tests"),
                         same.summary.stats = F,
                         same.lhs.vars = F)
cat(smd_panels, sep = '\n', file = "./Paper/appendix_materials/ilintab_smd.tex")

# Table B.2, controlling number of legs  Illinois/Indiana #######################################

state_mem <- felm(partyunity~bigger+majority+maj_share+inc+n_mem|state+mod_year|0|ko_id,data=dat_ilin)
id_mem <- felm(partyunity~bigger+majority+maj_share+inc+n_mem|ko_id+mod_year|0|ko_id,data=dat_ilin)
state_mem2 <- felm(partyunity~bigger*majority+maj_share+inc+n_mem|state+mod_year|0|ko_id,data=dat_ilin)
id_mem2 <- felm(partyunity~bigger*majority+maj_share+inc+n_mem|ko_id+mod_year|0|ko_id,data=dat_ilin)
state_mem3 <- felm(partyunity~bigger*n_mem+majority+maj_share+inc+n_mem|state+mod_year|0|ko_id,data=dat_ilin)
id_mem3 <- felm(partyunity~bigger*n_mem+majority+maj_share+inc+n_mem|ko_id+mod_year|0|ko_id,data=dat_ilin)

state_mem4 <- felm(partyunity~lht+majority+maj_share+inc+n_mem|state+mod_year|0|ko_id,data=dat_ilin)
id_mem4 <- felm(partyunity~lht+majority+maj_share+inc+n_mem|ko_id+mod_year|0|ko_id,data=dat_ilin)
state_mem5 <- felm(partyunity~lht*majority+maj_share+inc+n_mem|state+mod_year|0|ko_id,data=dat_ilin)
id_mem5 <- felm(partyunity~lht*majority+maj_share+inc+n_mem|ko_id+mod_year|0|ko_id,data=dat_ilin)
state_mem6 <- felm(partyunity~lht*n_mem+majority+maj_share+inc+n_mem|state+mod_year|0|ko_id,data=dat_ilin)
id_mem6 <- felm(partyunity~lht*n_mem+majority+maj_share+inc+n_mem|ko_id+mod_year|0|ko_id,data=dat_ilin)


ilinsg_numem <-  stargazer(state_mem,state_mem2,state_mem3,id_mem,id_mem2,id_mem3,
                           dep.var.labels = c("Party Unity Score"),
                           order=c(1,7,5,2,3,4,6),
                           covariate.labels = c("Chamber Larger",
                                                "Larger $\\times$ Majority",
                                                "Larger $\\times$ Number of Legislators",
                                                "In Majority",
                                                "Majority Seat Share", "Incumbent",
                                                "Number of Legislators"
                           ),
                           keep.stat = c("n"),
                           report = "vc*s",table.placement = "!ht",
                           add.lines = list(
                             c("Fixed Effects","State + Year","State + Year","State + Year","Leg. + Year","Leg. + Year","Leg. + Year")),
                           title = "Controlling for Multi-Member Districts: Illinois and Indiana, 1838 to 1851 \\vspace{-0.75em}",label="ilintab_numem",
                           notes.append = FALSE,notes.label = "", no.space=T,
                           digits=3,digits.extra=3,column.sep.width="-2pt",
                           omit.table.layout = "l#",
                           star.char=c("*","**"),star.cutoffs = c(.1,.05),font.size="footnotesize",
                           notes="\\parbox[t]{\\textwidth}{\\footnotesize \\textit{Note}: Entries are linear regression coefficients with 
                       standard errors clustered by legislator in parentheses. Observations are at the legislator-biennium level. 
              $^{**}$p$<$0.05, $^{*}$p$<$0.10 (two-tailed).}")

ilinsg_numem_cont <-  stargazer(state_mem4,state_mem5,state_mem6,
                                id_mem4,id_mem5,id_mem6,
                                dep.var.labels = c("Party Unity Score","Party Unity (80\\%)"),
                                order=c(1,7,5,2,3,4,6),
                                covariate.labels = c("Chamber Size",
                                                     "Size $\\times$ Majority",
                                                     "Size $\\times$ Number of Legislators",
                                                     "In Majority",
                                                     "Majority Seat Share", "Incumbent",
                                                     "Number of Legislators"
                                ),
                                keep.stat = c("n"),
                                report = "vc*s",table.placement = "!ht",font.size = "footnotesize",
                                add.lines = list(
                                  c("Fixed Effects","State + Year","State + Year","State + Year","Leg. + Year","Leg. + Year","Leg. + Year")),
                                title = "Incorporating the Number of Legislators Representing the District: Illinois and Indiana, 1838 to 1851  \\vspace{-0.75em}",label="ilintab_numem",
                                notes.append = FALSE,notes.label = "", no.space=T,
                                digits=3,digits.extra=3,column.sep.width="0pt",
                                omit.table.layout = "l#",
                                star.char=c("*","**"),star.cutoffs = c(.1,.05),
                                notes="\\parbox[t]{\\textwidth}{\\footnotesize \\textit{Note}: Entries are linear regression coefficients with 
                       standard errors clustered by legislator in parentheses. Observations are at the legislator-biennium level. 
              $^{**}$p$<$0.05, $^{*}$p$<$0.10 (two-tailed).}")

  numem_panels <- star_panel(ilinsg_numem,ilinsg_numem_cont,panel.label.fontface="bold",
                             panel.names = c("Binary Independent Variable", 
                                             "Continuous Independent Variable"),
                             same.summary.stats = T,
                             same.lhs.vars = F)
  cat(numem_panels, sep = '\n', file = "./Paper/appendix_materials/numem_control.tex")

# Table B.3 controlling for number of party unity votes Illinois/Indiana #############################################

# total votes

tot <- felm(partyunity~bigger+majority+maj_share+inc+tot_puvotes|state+mod_year|0|ko_id,data=dat_ilin)
inttot <- felm(partyunity~bigger*majority+maj_share+inc+tot_puvotes|state+mod_year|0|ko_id,data=dat_ilin)
tot2 <- felm(partyunity~bigger+majority+maj_share+inc+tot_puvotes|ko_id+mod_year|0|ko_id,data=dat_ilin)
inttot2 <- felm(partyunity~bigger*majority+maj_share+inc+tot_puvotes|ko_id+mod_year|0|ko_id,data=dat_ilin)

tot3 <- felm(partyunity~lht+majority+maj_share+inc+tot_puvotes|state+mod_year|0|ko_id,data=dat_ilin)
inttot3 <- felm(partyunity~lht*majority+maj_share+inc+tot_puvotes|state+mod_year|0|ko_id,data=dat_ilin)
tot4 <- felm(partyunity~lht+majority+maj_share+inc+tot_puvotes|ko_id+mod_year|0|ko_id,data=dat_ilin)
inttot4 <- felm(partyunity~lht*majority+maj_share+inc+tot_puvotes|ko_id+mod_year|0|ko_id,data=dat_ilin)

# individual votes

ind <- felm(partyunity~bigger+majority+maj_share+inc+partyunityvotes|state+mod_year|0|ko_id,data=dat_ilin)
intind <- felm(partyunity~bigger*majority+maj_share+inc+partyunityvotes|state+mod_year|0|ko_id,data=dat_ilin)
ind2 <- felm(partyunity~bigger+majority+maj_share+inc+partyunityvotes|ko_id+mod_year|0|ko_id,data=dat_ilin)
intind2 <- felm(partyunity~bigger*majority+maj_share+inc+partyunityvotes|ko_id+mod_year|0|ko_id,data=dat_ilin)

ind3 <- felm(partyunity~lht+majority+maj_share+inc+partyunityvotes|state+mod_year|0|ko_id,data=dat_ilin)
intind3 <- felm(partyunity~lht*majority+maj_share+inc+partyunityvotes|state+mod_year|0|ko_id,data=dat_ilin)
ind4 <- felm(partyunity~lht+majority+maj_share+inc+partyunityvotes|ko_id+mod_year|0|ko_id,data=dat_ilin)
intind4 <- felm(partyunity~lht*majority+maj_share+inc+partyunityvotes|ko_id+mod_year|0|ko_id,data=dat_ilin)

ilinsg_controlpu <-  stargazer(tot, ind,inttot,intind,
                               tot2,ind2,inttot2,intind2,
                               dep.var.labels = c("Party Unity Score"),
                               order=c(1,7,2,3,4,5,6),
                               covariate.labels = c("Chamber Larger",
                                                    "Larger $\\times$ In Maj.",
                                                    "In Majority",
                                                    "Majority Seat Share", "Incumbent", "Total P.U. Votes",
                                                    "Individual P.U. Votes"),
                               keep.stat = c("n"),
                               report = "vc*s",table.placement = "!ht",
                               add.lines = list(c("Fixed Effects","\\multicolumn{2}{c}{State + Year}","\\multicolumn{2}{c}{State + Year}",
                                                  "\\multicolumn{2}{c}{Legislator + Year}","\\multicolumn{2}{c}{Legislator + Year} \\\\ %")),
                               title = "Controlling for Number of Party Unity Votes: Illinois and Indiana, 1838 to 1851 \\vspace{-0.75em}",label="ilintab_pu",
                               notes.append = FALSE,notes.label = "", no.space=T,
                               digits=3,digits.extra=2,column.sep.width="0pt",font.size="footnotesize",
                               omit.table.layout = "l#",
                               star.char=c("*","**"),star.cutoffs = c(.1,.05),
                               notes="\\parbox[t]{\\textwidth}{\\footnotesize \\textit{Note}: Entries are linear regression coefficients with 
                       standard errors clustered by legislator in parentheses. Observations are at the legislator-biennium level. 
              $^{**}$p$<$0.05, $^{*}$p$<$0.10 (two-tailed).}")  

ilinsg_controlpu_cont <-  stargazer(tot3,ind3,inttot3,intind3,
                                    tot4,ind4,inttot4,intind4,
                                    dep.var.labels = c("Party Unity Score"),
                                    order=c(1,7,2,3,4,5,6),
                                    covariate.labels = c("Chamber Size",
                                                         "Size $\\times$ In Maj.",
                                                         "In Majority",
                                                         "Majority Seat Share", "Incumbent", "Total P.U. Votes",
                                                         "Individual P.U. Votes"),
                                    keep.stat = c("n"),
                                    report = "vc*s",table.placement = "!ht",
                                    add.lines = list(c("Fixed Effects","\\multicolumn{2}{c}{State + Year}","\\multicolumn{2}{c}{State + Year}",
                                                       "\\multicolumn{2}{c}{Legislator + Year}","\\multicolumn{2}{c}{Legislator + Year} \\\\ %")),
                                    title = "Controlling for Number of Party Unity Votes: Illinois and Indiana, 1838 to 1851 \\vspace{-0.75em}",label="ilintab_pu",
                                    notes.append = FALSE,notes.label = "", no.space=T,
                                    digits=3,digits.extra=3,column.sep.width="0pt",font.size="footnotesize",
                                    omit.table.layout = "l#",
                                    star.char=c("*","**"),star.cutoffs = c(.1,.05),
                                    notes="\\parbox[t]{\\textwidth}{\\footnotesize \\textit{Note}: Entries are linear regression coefficients with 
                       standard errors clustered by legislator in parentheses. Observations are at the legislator-biennium level. 
              $^{**}$p$<$0.05, $^{*}$p$<$0.10 (two-tailed).}")   

pu_control <- star_panel(ilinsg_controlpu,ilinsg_controlpu_cont,panel.label.fontface="bold",
                         panel.names = c("Binary Independent Variable", 
                                         "Continuous Independent Variable"),
                         same.lhs.vars = F,same.summary.stats = T)
cat(pu_control, sep = '\n', file = "./Paper/appendix_materials/pu_control.tex")

# Table B.4 weighting by individual party unity votes Illinois/Indiana ###################

weight <- felm(partyunity~bigger+majority+maj_share+inc|state+mod_year|0|ko_id,data=dat_ilin,weights=dat_ilin$partyunityvotes)
int_weight <- felm(partyunity~bigger*majority+maj_share+inc|state+mod_year|0|ko_id,data=dat_ilin,weights=dat_ilin$partyunityvotes)
weight2 <- felm(partyunity~bigger+majority+maj_share+inc|ko_id+mod_year|0|ko_id,data=dat_ilin,weights=dat_ilin$partyunityvotes)
int_weight2 <- felm(partyunity~bigger*majority+maj_share+inc|ko_id+mod_year|0|ko_id,data=dat_ilin,weights=dat_ilin$partyunityvotes)

weight_cont <- felm(partyunity~lht+majority+maj_share+inc|state+mod_year|0|ko_id,data=dat_ilin,weights=dat_ilin$partyunityvotes)
int_weight_cont <- felm(partyunity~lht*majority+maj_share+inc|state+mod_year|0|ko_id,data=dat_ilin,weights=dat_ilin$partyunityvotes)
weight_cont2 <- felm(partyunity~lht+majority+maj_share+inc|ko_id+mod_year|0|ko_id,data=dat_ilin,weights=dat_ilin$partyunityvotes)
int_weight_cont2 <- felm(partyunity~lht*majority+maj_share+inc|ko_id+mod_year|0|ko_id,data=dat_ilin,weights=dat_ilin$partyunityvotes)

ilinsg_weight_pu <-  stargazer(weight,int_weight,weight2,int_weight2,
                               dep.var.labels = c("Party Unity Score"),
                               order=c(1,5,2,3,4),
                               covariate.labels = c("Chamber Larger",
                                                    "Larger $\\times$ In Maj.",
                                                    "In Majority",
                                                    "Majority Seat Share", "Incumbent"),
                               keep.stat = c("n"),
                               report = "vc*s",table.placement = "!ht",
                               add.lines = list(c("Fixed Effects","\\multicolumn{2}{c}{State + Year}",
                                                  "\\multicolumn{2}{c}{Legislator + Year} \\\\ %")),
                               title = "Weighting by Number of Party Unity Votes: Illinois and Indiana, 1838 to 1851 \\vspace{-0.75em}",label="ilintab_pu_weight",
                               notes.append = FALSE,notes.label = "", no.space=T,
                               digits=3,digits.extra=3,
                               omit.table.layout = "l#",
                               star.char=c("*","**"),star.cutoffs = c(.1,.05),
                               notes="\\parbox[t]{0.75\\textwidth}{\\footnotesize \\textit{Note}: Entries are linear regression coefficients with 
                       standard errors clustered by legislator in parentheses. Observations are at the legislator-biennium level. 
              $^{**}$p$<$0.05, $^{*}$p$<$0.10 (two-tailed).}")   

ilinsg_weight_pu_cont <-  stargazer(weight_cont,int_weight_cont,weight_cont2,int_weight_cont2,
                                    dep.var.labels = c("Party Unity Score"),
                                    order=c(1,5,2,3,4),
                                    covariate.labels = c("Chamber Size",
                                                         "Size $\\times$ In Maj.",
                                                         "In Majority",
                                                         "Majority Seat Share", "Incumbent"),
                                    keep.stat = c("n"),
                                    report = "vc*s",table.placement = "!ht",
                                    add.lines = list(c("Fixed Effects","\\multicolumn{2}{c}{State + Year}",
                                                       "\\multicolumn{2}{c}{Legislator + Year} \\\\ %")),
                                    title = "Weighting by Number of Party Unity Votes: Illinois and Indiana, 1838 to 1851 \\vspace{-0.75em}",label="ilintab_pu_weight",
                                    notes.append = FALSE,notes.label = "", no.space=T,
                                    digits=3,digits.extra=3,
                                    omit.table.layout = "l#",
                                    star.char=c("*","**"),star.cutoffs = c(.1,.05),
                                    notes="\\parbox[t]{0.75\\textwidth}{\\footnotesize \\textit{Note}: Entries are weighted least squares
                                  estimates with standard errors clustered by legislator in parentheses. Observations 
                                  are at the legislator-biennium level and are weighted by the legislator's individual number of party
                                  unity votes participated in.
              $^{**}$p$<$0.05, $^{*}$p$<$0.10 (two-tailed).}")   

pu_weight <- star_panel(ilinsg_weight_pu,ilinsg_weight_pu_cont,panel.label.fontface="bold",
                        panel.names = c("Binary Independent Variable", 
                                        "Continuous Independent Variable"),
                        same.lhs.vars = F,same.summary.stats = T)
cat(pu_weight, sep = '\n', file = "./Paper/appendix_materials/pu_weight.tex")


# Table B.5 controlling for party unity votes, New Jersey/Vermont ############################################

# total party unity votes

nv1 <- felm(partyunity~bigger+majority+maj_share+inc+tot_puvotes|state+modyear1|0|id,data=dat_njvt)
nv2 <- felm(partyunity~bigger*majority+maj_share+inc+tot_puvotes|state+modyear1|0|id,data=dat_njvt)
nv3 <- felm(partyunity~bigger+majority+maj_share+inc+tot_puvotes|id+modyear1|0|id,data=dat_njvt)
nv4 <- felm(partyunity~bigger*majority+maj_share+inc+tot_puvotes|id+modyear1|0|id,data=dat_njvt)

nvtot1 <- felm(partyunity~lht+majority+maj_share+inc+tot_puvotes|state+modyear1|0|id,data=dat_njvt)
nvtot2 <- felm(partyunity~lht*majority+maj_share+inc+tot_puvotes|state+modyear1|0|id,data=dat_njvt)
nvtot3 <- felm(partyunity~lht+majority+maj_share+inc+tot_puvotes|id+modyear1|0|id,data=dat_njvt)
nvtot4 <- felm(partyunity~lht*majority+maj_share+inc+tot_puvotes|id+modyear1|0|id,data=dat_njvt)

# individual party unity votes

nv_ind1 <- felm(partyunity~bigger+majority+maj_share+inc+partyunityvotes|state+modyear1|0|id,data=dat_njvt)
nv_ind2 <- felm(partyunity~bigger*majority+maj_share+inc+partyunityvotes|state+modyear1|0|id,data=dat_njvt)
nv_ind3 <- felm(partyunity~bigger+majority+maj_share+inc+partyunityvotes|id+modyear1|0|id,data=dat_njvt)
nv_ind4 <- felm(partyunity~bigger*majority+maj_share+inc+partyunityvotes|id+modyear1|0|id,data=dat_njvt)

nvc1 <- felm(partyunity~lht+majority+maj_share+inc+partyunityvotes|state+modyear1|0|id,data=dat_njvt)
nvc2 <- felm(partyunity~lht*majority+maj_share+inc+partyunityvotes|state+modyear1|0|id,data=dat_njvt)
nvc3 <- felm(partyunity~lht+majority+maj_share+inc+partyunityvotes|id+modyear1|0|id,data=dat_njvt)
nvc4 <- felm(partyunity~lht*majority+maj_share+inc+partyunityvotes|id+modyear1|0|id,data=dat_njvt)

njvtsg_pu <-  stargazer(nv1,nv_ind1,nv2,nv_ind2,
                        nv3,nv_ind3,nv4,nv_ind4,
                        dep.var.labels = c("Party Unity Score"),
                        order=c(1,7,2,3,4,5,6),
                        covariate.labels = c("Chamber Larger",
                                             "Larger $\\times$ In Maj.",
                                             "In Majority",
                                             "Majority Seat Share", "Incumbent",
                                             "Total P.U. Votes",
                                             "Individual P.U. Votes"),
                        keep.stat = c("n"),report = "vc*s",table.placement = "!ht",
                        add.lines = list(c("Fixed Effects","\\multicolumn{2}{c}{State + Year}","\\multicolumn{2}{c}{State + Year}"
                                           ,"\\multicolumn{2}{c}{Legislator + Year}","\\multicolumn{2}{c}{Legislator + Year} \\\\ %")),
                        title = "Controlling for Number of Party Unity Votes: New Jersey and Vermont, 1957-1974 \\vspace{-0.75em}",label="njvttab_pu",
                        notes.append = FALSE,notes.label = "", no.space=T,
                        omit.table.layout = "l#",
                        digits=3,digits.extra=3,font.size="footnotesize",column.sep.width = "0pt",
                        star.char=c("*","**"),star.cutoffs = c(.1,.05),
                        notes="\\parbox[t]{\\textwidth}{\\footnotesize \\textit{Note}: Entries are linear regression coefficients with 
                     standard errors clustered by legislator in parentheses. Observations are at the legislator-biennium level. $^{**}$p$<$0.05, $^{*}$p$<$0.10 (two-tailed).}")   

njvtsg_pu_cont <-  stargazer(nvtot1,nvc1,nvtot2,nvc2,
                             nvtot3,nvc3,nvtot4,nvc4,
                             dep.var.labels = c("Party Unity Score"),
                             order=c(1,7,2,3,4,5,6),
                             covariate.labels = c("Chamber Size",
                                                  "Size $\\times$ In Maj.",
                                                  "In Majority",
                                                  "Majority Seat Share", "Incumbent",
                                                  "Total P.U. Votes",
                                                  "Individual P.U. Votes"),
                             keep.stat = c("n"),report = "vc*s",table.placement = "!ht",
                             add.lines = list(c("Fixed Effects","\\multicolumn{2}{c}{State + Year}","\\multicolumn{2}{c}{State + Year}"
                                                ,"\\multicolumn{2}{c}{Legislator + Year}","\\multicolumn{2}{c}{Legislator + Year} \\\\ %")),
                             title = "Controlling for Number of Party Unity Votes: New Jersey and Vermont, 1957-1974 \\vspace{-0.75em}",label="njvttab_pu",
                             notes.append = FALSE,notes.label = "", no.space=T,
                             omit.table.layout = "l#",
                             digits=3,digits.extra=3,font.size="footnotesize",column.sep.width = "0pt",
                             star.char=c("*","**"),star.cutoffs = c(.1,.05),
                             notes="\\parbox[t]{\\textwidth}{\\footnotesize \\textit{Note}: Entries are linear regression coefficients with 
                     standard errors clustered by legislator in parentheses. Observations are at the legislator-biennium level. $^{**}$p$<$0.05, $^{*}$p$<$0.10 (two-tailed).}")   

njvt_pu_panels <- star_panel(njvtsg_pu,njvtsg_pu_cont,panel.label.fontface="bold",
                             panel.names = c("Binary Independent Variable", 
                                             "Continuous Independent Variable"),
                             same.lhs.vars = F,same.summary.stats = T)
cat(njvt_pu_panels, sep = '\n', file = "./Paper/appendix_materials/njvt_pu_control.tex")

# Table B.6 weighting by party unity votes New Jersey/Vermont ###############################################

njvt_weight1 <- felm(partyunity~bigger+majority+maj_share+inc|state+modyear1|0|id,data=dat_njvt,weights=dat_njvt$partyunityvotes)
njvt_weight2 <- felm(partyunity~bigger*majority+maj_share+inc|state+modyear1|0|id,data=dat_njvt,weights=dat_njvt$partyunityvotes)
njvt_weight3 <- felm(partyunity~bigger+majority+maj_share+inc|id+modyear1|0|id,data=dat_njvt,weights=dat_njvt$partyunityvotes)
njvt_weight4 <- felm(partyunity~bigger*majority+maj_share+inc|id+modyear1|0|id,data=dat_njvt,weights=dat_njvt$partyunityvotes)

njvt_weight5 <- felm(partyunity~lht+majority+maj_share+inc|state+modyear1|0|id,data=dat_njvt,weights=dat_njvt$partyunityvotes)
njvt_weight6 <- felm(partyunity~lht*majority+maj_share+inc|state+modyear1|0|id,data=dat_njvt,weights=dat_njvt$partyunityvotes)
njvt_weight7 <- felm(partyunity~lht+majority+maj_share+inc|id+modyear1|0|id,data=dat_njvt,weights=dat_njvt$partyunityvotes)
njvt_weight8 <- felm(partyunity~lht*majority+maj_share+inc|id+modyear1|0|id,data=dat_njvt,weights=dat_njvt$partyunityvotes)

njvtsg_weight <-  stargazer(njvt_weight1 ,njvt_weight2,njvt_weight3,njvt_weight4,
                            dep.var.labels = c("Party Unity Score"),
                            order=c(1,5,2,3,4),
                            covariate.labels = c("Chamber Larger",
                                                 "Larger $\\times$ In Maj.",
                                                 "In Majority",
                                                 "Majority Seat Share", "Incumbent"),
                            keep.stat = c("n"),report = "vc*s",table.placement = "!ht",
                            add.lines = list(c("Fixed Effects","\\multicolumn{2}{c}{State + Year}","\\multicolumn{2}{c}{Leg. + Year} \\\\ %")),
                            title = "Weighting by Number of Party Unity Votes: New Jersey and Vermont, 1957-1974 \\vspace{-0.75em}",label="njvttab_pu_weight",
                            notes.append = FALSE,notes.label = "", no.space=T,
                            omit.table.layout = "l#",
                            digits=3,digits.extra=3,
                            star.char=c("*","**"),star.cutoffs = c(.1,.05),
                            notes="\\parbox[t]{0.75\\textwidth}{\\footnotesize \\textit{Note}: Entries are linear regression coefficients with 
                     standard errors clustered by legislator in parentheses. Observations are at the legislator-biennium level. 
            $^{**}$p$<$0.05, $^{*}$p$<$0.10 (two-tailed).}")   

njvtsg_weight_cont <-  stargazer(njvt_weight5,njvt_weight6,njvt_weight7,njvt_weight8,
                                 dep.var.labels = c("Party Unity Score"),
                                 order=c(1,5,2,3,4),
                                 covariate.labels = c("Chamber Size",
                                                      "Larger $\\times$ In Maj.",
                                                      "In Majority",
                                                      "Majority Seat Share", "Incumbent"),
                                 keep.stat = c("n"),report = "vc*s",table.placement = "!ht",
                                 add.lines = list(c("Fixed Effects","\\multicolumn{2}{c}{State + Year}","\\multicolumn{2}{c}{Leg. + Year} \\\\ %")),
                                 title = "Weighting by Number of Party Unity Votes: New Jersey and Vermont, 1957-1974 \\vspace{-0.75em}",label="njvttab_pu_weight",
                                 notes.append = FALSE,notes.label = "", no.space=T,
                                 omit.table.layout = "l#",
                                 digits=3,digits.extra=3,
                                 star.char=c("*","**"),star.cutoffs = c(.1,.05),
                                 notes="\\parbox[t]{0.75\\textwidth}{\\footnotesize \\textit{Note}: Entries are linear regression coefficients with 
                     standard errors clustered by legislator in parentheses. Observations are at the legislator-biennium level. 
            $^{**}$p$<$0.05, $^{*}$p$<$0.10 (two-tailed).}")  

njvtsg_weight_panel <- star_panel(njvtsg_weight,njvtsg_weight_cont,panel.label.fontface="bold",
                                  panel.names = c("Binary Independent Variable", 
                                                  "Continuous Independent Variable"),
                                  same.lhs.vars = F,same.summary.stats = T)
cat(njvtsg_weight_panel, sep = '\n', file = "./Paper/appendix_materials/njvt_pu_weight.tex")

# Table B.7 lagged dv Illinois/Indiana ##############################################################

ilin_dv <- felm(partyunity~bigger+majority+maj_share+inc+partyunity_lagged|mod_year|0|ko_id,data=dat_ilin)
ilin_int_dv <- felm(partyunity~bigger*majority+maj_share+inc+partyunity_lagged|mod_year|0|ko_id,data=dat_ilin)
ilin_state_dv <- felm(partyunity~bigger+majority+maj_share+inc+partyunity_lagged|state+mod_year|0|ko_id,data=dat_ilin)
ilin_state_int_dv <- felm(partyunity~bigger*majority+maj_share+inc+partyunity_lagged|state+mod_year|0|ko_id,data=dat_ilin)

ilin_dv_cont <- felm(partyunity~lht+majority+maj_share+inc+partyunity_lagged|mod_year|0|ko_id,data=dat_ilin)
ilin_int_dv_cont <- felm(partyunity~lht*majority+maj_share+inc+partyunity_lagged|mod_year|0|ko_id,data=dat_ilin)
ilin_state_dv_cont <- felm(partyunity~lht+majority+maj_share+inc+partyunity_lagged|state+mod_year|0|ko_id,data=dat_ilin)
ilin_state_int_dv_cont <- felm(partyunity~lht*majority+maj_share+inc+partyunity_lagged|state+mod_year|0|ko_id,data=dat_ilin)

ilinsg_dv <-  stargazer(ilin_dv,ilin_int_dv,ilin_state_dv,ilin_state_int_dv,
                        dep.var.labels = c("Party Unity Score"),
                        order=c(1,6,2,3,4,5),
                        covariate.labels = c("Chamber Larger",
                                             "Larger $\\times$ In Maj.",
                                             "In Majority",
                                             "Majority Seat Share", "Incumbent",
                                             "Lagged Party Unity Score"),
                        keep.stat = c("n"),report = "vc*s",table.placement = "!ht",
                        add.lines = list(c("Fixed Effects","\\multicolumn{2}{c}{Year}",
                                           "\\multicolumn{2}{c}{State + Year} \\\\ %")),
                        title = "Lagged Dependent Variable Models: Illinois and Indiana, 1838 to 1851 \\vspace{-0.75em}",label="ilindvtab",
                        notes.append = FALSE,notes.label = "", no.space=T,
                        digits=3,digits.extra=3,
                        omit.table.layout = "l#",
                        star.char=c("*","**"),star.cutoffs = c(.1,.05),
                        notes="\\parbox[t]{0.75\\textwidth}{\\footnotesize \\textit{Note}: Entries are linear regression coefficients with 
                       standard errors clustered by legislator in parentheses. Observations are at the legislator-biennium level. 
              $^{**}$p$<$0.05, $^{*}$p$<$0.10 (two-tailed).}")   

ilinsg_dv_cont <-  stargazer(ilin_dv_cont,ilin_int_dv_cont,ilin_state_dv_cont,ilin_state_int_dv_cont,
                             dep.var.labels = c("Party Unity Score"),
                             order=c(1,6,2,3,4,5),
                             covariate.labels = c("Chamber Size",
                                                  "Larger $\\times$ In Maj.",
                                                  "In Majority",
                                                  "Majority Seat Share", "Incumbent",
                                                  "Lagged Party Unity Score"),
                             keep.stat = c("n"),report = "vc*s",table.placement = "!ht",
                             add.lines = list(c("Fixed Effects","\\multicolumn{2}{c}{Year}",
                                                "\\multicolumn{2}{c}{State + Year} \\\\ %")),
                             title = "Lagged Dependent Variable Models: Illinois and Indiana, 1838 to 1851 \\vspace{-0.75em}",label="ilindvtab",
                             notes.append = FALSE,notes.label = "", no.space=T,
                             digits=3,digits.extra=3,
                             omit.table.layout = "l#",
                             star.char=c("*","**"),star.cutoffs = c(.1,.05),
                             notes="\\parbox[t]{0.75\\textwidth}{\\footnotesize \\textit{Note}: Entries are linear regression coefficients with 
                       standard errors clustered by legislator in parentheses. Observations are at the legislator-biennium level. 
              $^{**}$p$<$0.05, $^{*}$p$<$0.10 (two-tailed).}")    

inil_dv_panels <- star_panel(ilinsg_dv,ilinsg_dv_cont,panel.label.fontface="bold",
                             panel.names = c("Binary Independent Variable", 
                                             "Continuous Independent Variable"),
                             same.lhs.vars = F,same.summary.stats = T)
cat(inil_dv_panels, sep = '\n', file = "./Paper/appendix_materials/ilintab_dv.tex")


# Table B.8 lagged dv models New Jersey/Vermont #############################################################

njvt2_ldv <- felm(partyunity~bigger+majority+maj_share+inc+partyunity_lagged|modyear1|0|id,data=dat_njvt)
njvtint2_ldv <- felm(partyunity~bigger*majority+maj_share+inc+partyunity_lagged|modyear1|0|id,data=dat_njvt)
njvt3_ldv <- felm(partyunity~bigger+majority+maj_share+inc+partyunity_lagged|state+modyear1|0|id,data=dat_njvt)
njvtint1_ldv <- felm(partyunity~bigger*majority+maj_share+inc+partyunity_lagged|state+modyear1|0|id,data=dat_njvt)

njvt2_cont_ldv <- felm(partyunity~lht+majority+maj_share+inc+partyunity_lagged|modyear1|0|id,data=dat_njvt)
njvtint2_cont_ldv <- felm(partyunity~lht*majority+maj_share+inc+partyunity_lagged|modyear1|0|id,data=dat_njvt)
njvt3_cont_ldv <- felm(partyunity~lht+majority+maj_share+inc+partyunity_lagged|state+modyear1|0|id,data=dat_njvt)
njvtint1_cont_ldv <- felm(partyunity~lht*majority+maj_share+inc+partyunity_lagged|state+modyear1|0|id,data=dat_njvt)

njvtsg_ldv <-  stargazer(njvt2_ldv,njvtint2_ldv,njvt3_ldv,njvtint1_ldv,
                         dep.var.labels = c("Party Unity Score","Party Unity (80\\%)"),
                         order=c(1,6,2,3,4,5),
                         covariate.labels = c("Chamber Larger",
                                              "Larger $\\times$ In Maj.",
                                              "In Majority",
                                              "Majority Seat Share", "Incumbent",
                                              "Lagged Party Unity Score"),
                         keep.stat = c("n"),report = "vc*s",table.placement = "!ht",
                         add.lines = list(c("Fixed Effects","\\multicolumn{2}{c}{Year}","\\multicolumn{2}{c}{State + Year} \\\\ %")),
                         title = "Lagged Dependent Variable Models: New Jersey and Vermont, 1957-1974 \\vspace{-0.75em}",label="njvttab_ldv",
                         notes.append = FALSE,notes.label = "", no.space=T,
                         omit.table.layout = "l#",
                         digits=3,digits.extra=3,
                         star.char=c("*","**"),star.cutoffs = c(.1,.05),
                         notes="\\parbox[t]{0.775\\textwidth}{\\footnotesize \\textit{Note}: Entries are linear regression coefficients with 
                       standard errors clustered by legislator in parentheses. Observations are at the legislator-biennium level. 
              $^{**}$p$<$0.05, $^{*}$p$<$0.10 (two-tailed).}")   

njvtsg_cont_ldv <-  stargazer(njvt2_cont_ldv,njvtint2_cont_ldv,njvt3_cont_ldv,njvtint1_cont_ldv,
                              dep.var.labels = c("Party Unity Score","Party Unity (80\\%)"),
                              order=c(1,6,2,3,4,5),
                              covariate.labels = c("Chamber Size",
                                                   "Larger $\\times$ In Maj.",
                                                   "In Majority",
                                                   "Majority Seat Share", "Incumbent",
                                                   "Lagged Party Unity Score"),
                              keep.stat = c("n"),report = "vc*s",table.placement = "!ht",
                              add.lines = list(c("Fixed Effects","\\multicolumn{2}{c}{Year}","\\multicolumn{2}{c}{State + Year} \\\\ %")),
                              title = "Lagged Dependent Variable Models: New Jersey and Vermont, 1957-1974 \\vspace{-0.75em}",label="njvttab_ldv",
                              notes.append = FALSE,notes.label = "", no.space=T,
                              omit.table.layout = "l#",
                              digits=3,digits.extra=3,
                              star.char=c("*","**"),star.cutoffs = c(.1,.05),
                              notes="\\parbox[t]{0.775\\textwidth}{\\footnotesize \\textit{Note}: Entries are linear regression coefficients with 
                       standard errors clustered by legislator in parentheses. Observations are at the legislator-biennium level. 
              $^{**}$p$<$0.05, $^{*}$p$<$0.10 (two-tailed).}")  

njvt_ldv <- star_panel(njvtsg_ldv,njvtsg_cont_ldv,panel.label.fontface="bold",
                       panel.names = c("Binary Independent Variable", 
                                       "Continuous Independent Variable"),
                       same.lhs.vars = F,same.summary.stats = T)
cat(njvt_ldv, sep = '\n', file = "./Paper/appendix_materials/njvt_ldv_tab.tex")  


# Table B.9 using a smooth polynomial of year Illinois/Indiana ############################################

dat_ilin$lowyear <- dat_ilin$year-(min(dat_ilin$year,na.rm=T)-1) # this just helps us avoid calculation issues

ilin2_y3 <- felm(partyunity~bigger+majority+maj_share+inc+lowyear+I(lowyear^2)|state|0|ko_id,data=dat_ilin)
ilinint2_y3 <- felm(partyunity~bigger*majority+maj_share+inc+lowyear+I(lowyear^2)|state|0|ko_id,data=dat_ilin)
ilin3_y3 <- felm(partyunity~bigger+majority+maj_share+inc+lowyear+I(lowyear^2)|ko_id|0|ko_id,data=dat_ilin)
ilinint1_y3 <- felm(partyunity~bigger*majority+maj_share+inc+lowyear+I(lowyear^2)|ko_id|0|ko_id,data=dat_ilin)

ilin2_y3_cont <- felm(partyunity~lht+majority+maj_share+inc+lowyear+I(lowyear^2)|state|0|ko_id,data=dat_ilin)
ilinint2_y3_cont <- felm(partyunity~lht*majority+maj_share+inc+lowyear+I(lowyear^2)|state|0|ko_id,data=dat_ilin)
ilin3_y3_cont <- felm(partyunity~lht+majority+maj_share+inc+lowyear+I(lowyear^2)|ko_id|0|ko_id,data=dat_ilin)
ilinint1_y3_cont <- felm(partyunity~lht*majority+maj_share+inc+lowyear+I(lowyear^2)|ko_id|0|ko_id,data=dat_ilin)

ilinsg_years <-  stargazer(ilin2_y3,ilinint2_y3,ilin3_y3,ilinint1_y3,
                           dep.var.labels = c("Party Unity Score"),
                           order=c(1,7,2,3,4,5,6),
                           covariate.labels = c("Chamber Larger",
                                                "Larger $\\times$ In Maj.",
                                                "In Majority",
                                                "Majority Seat Share", "Incumbent",
                                                "Year","Year$^2$"),
                           keep.stat = c("n"),report = "vc*s",table.placement = "H",
                           add.lines = list(c("Fixed Effects",
                                              "\\multicolumn{2}{c}{State}",
                                              "\\multicolumn{2}{c}{Legislator} \\\\ %")),
                           title = "Models with Polynomial Fits of Time: Illinois and Indiana, 1838 to 1851 \\vspace{-0.75em}",label="ilintab_year",
                           notes.append = FALSE,notes.label = "", no.space=T,font.size="footnotesize",
                           digits=3,digits.extra=3,
                           omit.table.layout ="l#",
                           star.char=c("*","**"),star.cutoffs = c(.1,.05),
                           notes="\\parbox[t]{0.65\\textwidth}{\\footnotesize \\textit{Note}: Entries are linear regression coefficients with 
                       standard errors clustered by legislator in parentheses. The ``Year'' variable is defined as ``year-1837'' for computational purposes.
                       Observations are at the legislator-biennium level. 
              $^{**}$p$<$0.05, $^{*}$p$<$0.10 (two-tailed).}")   

ilinsg_years_cont <-  stargazer(ilin2_y3_cont,ilinint2_y3_cont,ilin3_y3_cont,ilinint1_y3_cont,
                                dep.var.labels = c("Party Unity Score"),
                                order=c(1,7,2,3,4,5,6),
                                covariate.labels = c("Chamber Size",
                                                     "Size $\\times$ In Maj.",
                                                     "In Majority",
                                                     "Majority Seat Share", "Incumbent",
                                                     "Year","Year$^2$"),
                                keep.stat = c("n"),report = "vc*s",table.placement = "H",
                                add.lines = list(c("Fixed Effects",
                                                   "\\multicolumn{2}{c}{State}",
                                                   "\\multicolumn{2}{c}{Legislator} \\\\ %")),
                                title = "Models with Polynomial Fits of Time: Illinois and Indiana, 1838 to 1851 \\vspace{-0.75em}",label="ilintab_year",
                                notes.append = FALSE,notes.label = "", no.space=T,font.size="footnotesize",
                                digits=3,digits.extra=3,
                                omit.table.layout ="l#",
                                star.char=c("*","**"),star.cutoffs = c(.1,.05),
                                notes="\\parbox[t]{0.65\\textwidth}{\\footnotesize \\textit{Note}: Entries are linear regression coefficients with 
                     standard errors clustered by legislator in parentheses. The ``Year'' variable is defined as ``year-1837'' for computational purposes.
                     Observations are at the legislator-biennium level. 
            $^{**}$p$<$0.05, $^{*}$p$<$0.10 (two-tailed).}")   

ilinsg_years_panel <- star_panel(ilinsg_years,ilinsg_years_cont,panel.label.fontface="bold",
                                 panel.names = c("Binary Independent Variable", 
                                                 "Continuous Independent Variable"),
                                 same.lhs.vars = F,same.summary.stats = T)

cat(ilinsg_years_panel, sep = '\n', file = "./Paper/appendix_materials/ilintab_years.tex")


# Table B.10 using a smooth polynomial of year New Jersey/Vermont ############################################

dat_njvt$lowyear <- dat_njvt$year-(min(dat_njvt$year,na.rm=T)-1) # this just helps us avoid calculation issues

njvt1_y2_bin <- felm(partyunity~bigger+majority+maj_share+inc+lowyear+I(lowyear^2)|state|0|id,data=dat_njvt)
njvtint1_y2_bin <- felm(partyunity~bigger*majority+maj_share+inc+lowyear+I(lowyear^2)|state|0|id,data=dat_njvt)
njvt3_y2_bin <- felm(partyunity~bigger+majority+maj_share+inc+lowyear+I(lowyear^2)|id|0|id,data=dat_njvt)
njvtint3_y2_bin <- felm(partyunity~bigger*majority+maj_share+inc+lowyear+I(lowyear^2)|id|0|id,data=dat_njvt)

njvt1_y2_cont <- felm(partyunity~lht+majority+maj_share+inc+lowyear+I(lowyear^2)|state|0|id,data=dat_njvt)
njvtint1_y2_cont <- felm(partyunity~lht*majority+maj_share+inc+lowyear+I(lowyear^2)|state|0|id,data=dat_njvt)
njvt3_y2_cont <- felm(partyunity~lht+majority+maj_share+inc+lowyear+I(lowyear^2)|id|0|id,data=dat_njvt)
njvtint3_y2_cont <- felm(partyunity~lht*majority+maj_share+inc+lowyear+I(lowyear^2)|id|0|id,data=dat_njvt)

njvtsg_years_bin <-  stargazer(njvt1_y2_bin,njvtint1_y2_bin,njvt3_y2_bin,njvtint3_y2_bin,
                               dep.var.labels = c("Party Unity Score"),
                               order=c(1,7,2,3,4,5,6),
                               covariate.labels = c("Chamber Larger",
                                                    "Larger $\\times$ In Maj.",
                                                    "In Majority",
                                                    "Majority Seat Share", "Incumbent",
                                                    "Year","Year$^2$"),
                               keep.stat = c("n"),report = "vc*s",table.placement = "!ht",
                               add.lines = list(c("Fixed Effects",
                                                  "\\multicolumn{2}{c}{State}",
                                                  "\\multicolumn{2}{c}{Legislator} \\\\ %")),
                               title = "Models with Polynomial Fits of Time: New Jersey and Vermont, 1957 to 1974 \\vspace{-0.75em}",label="njvttab_year",
                               notes.append = FALSE,notes.label = "", no.space=T,font.size="footnotesize",
                               omit.table.layout = c("l#"),
                               digits=3,digits.extra=3,
                               star.char=c("*","**"),star.cutoffs = c(.1,.05),
                               notes="\\parbox[t]{0.625\\textwidth}{\\footnotesize \\textit{Note}: Entries are linear regression coefficients with 
                       standard errors clustered by legislator in parentheses. The ``Year'' variable is defined as ``year-1956'' for computational purposes.
                       Observations are at the legislator-biennium level. 
              $^{**}$p$<$0.05, $^{*}$p$<$0.10 (two-tailed).}")   

njvtsg_years_cont <-  stargazer(njvt1_y2_cont,njvtint1_y2_cont,njvt3_y2_cont,njvtint3_y2_cont,
                                dep.var.labels = c("Party Unity Score"),
                                order=c(1,7,2,3,4,5,6),
                                covariate.labels = c("Chamber Size",
                                                     "Size $\\times$ In Maj.",
                                                     "In Majority",
                                                     "Majority Seat Share", "Incumbent",
                                                     "Year","Year$^2$"),
                                keep.stat = c("n"),report = "vc*s",table.placement = "!ht",
                                add.lines = list(c("Fixed Effects",
                                                   "\\multicolumn{2}{c}{State}",
                                                   "\\multicolumn{2}{c}{Legislator} \\\\ %")),
                                title = "Models with Polynomial Fits of Time: New Jersey and Vermont, 1957 to 1974 \\vspace{-0.75em}",label="njvttab_year",
                                notes.append = FALSE,notes.label = "", no.space=T,font.size="footnotesize",
                                omit.table.layout = c("l#"),
                                digits=3,digits.extra=3,
                                star.char=c("*","**"),star.cutoffs = c(.1,.05),
                                notes="\\parbox[t]{0.625\\textwidth}{\\footnotesize \\textit{Note}: Entries are linear regression coefficients with 
                       standard errors clustered by legislator in parentheses. The ``Year'' variable is defined as ``year-1956'' for computational purposes.
                       Observations are at the legislator-biennium level. 
              $^{**}$p$<$0.05, $^{*}$p$<$0.10 (two-tailed).}")   

njvt_altyears <- star_panel(njvtsg_years_bin,njvtsg_years_cont,panel.label.fontface="bold",
                            panel.names = c("Binary Independent Variable", 
                                            "Continuous Independent Variable"),
                            same.lhs.vars = F,same.summary.stats = T)
cat(njvt_altyears, sep = '\n', file = "./Paper/appendix_materials/njvt_years.tex")

# Table B.11 controlling for party - both Il/IN and NJ/VT ############################################

dat_ilin$whig <- ifelse(dat_ilin$party=="w",1,0)

ilin_party1 <- felm(partyunity~bigger+majority+maj_share+inc+whig|state+mod_year|0|ko_id,data=dat_ilin)
ilin_party2 <- felm(partyunity~bigger*majority+maj_share+inc+whig|state+mod_year|0|ko_id,data=dat_ilin)
ilin_party3 <- felm(partyunity~bigger+majority+maj_share+inc+whig*factor(state)-factor(state)|state+mod_year|0|ko_id,data=dat_ilin)
ilin_party4 <- felm(partyunity~bigger*majority+maj_share+inc+whig*factor(state)-factor(state)|state+mod_year|0|ko_id,data=dat_ilin)

dat_njvt$rep <- ifelse(dat_njvt$party=="r",1,0)

njvt_party1 <- felm(partyunity~bigger+majority+maj_share+inc+rep|state+modyear1|0|id,data=dat_njvt)
njvt_party2 <- felm(partyunity~bigger*majority+maj_share+inc+rep|state+modyear1|0|id,data=dat_njvt)
njvt_party3 <- felm(partyunity~bigger+majority+maj_share+inc+rep*factor(state)-factor(state)|state+modyear1|0|id,data=dat_njvt)
njvt_party4 <- felm(partyunity~bigger*majority+maj_share+inc+rep*factor(state)-factor(state)|state+modyear1|0|id,data=dat_njvt)

ilin_party <-  stargazer(ilin_party1,ilin_party2,ilin_party3,ilin_party4,
                         dep.var.labels = c("Party Unity Score"),
                         order=c(1,6,2,3,4,5,7),
                         covariate.labels = c("Chamber Larger",
                                              "Larger $\\times$ In Maj.",
                                              "In Majority",
                                              "Majority Seat Share", "Incumbent",
                                              "Whig","Whig $\\times$ Indiana"),
                         keep.stat = c("n"),report = "vc*s",table.placement = "!ht",
                         add.lines = list(c("Fixed Effects",
                                            "\\multicolumn{2}{c}{State+Year}",
                                            "\\multicolumn{2}{c}{State+Year} \\\\ %")),
                         title = "Controlling for Individual Party Affiliation \\vspace{-0.75em}",label="partyaff",
                         notes.append = FALSE,notes.label = "", no.space=T,font.size="footnotesize",
                         omit.table.layout = c("l#"),
                         digits=3,digits.extra=3,
                         star.char=c("*","**"),star.cutoffs = c(.1,.05),
                         notes="\\parbox[t]{0.625\\textwidth}{\\footnotesize \\textit{Note}: Entries are linear regression coefficients with 
                       standard errors clustered by legislator in parentheses. 
                       Observations are at the legislator-biennium level. 
              $^{**}$p$<$0.05, $^{*}$p$<$0.10 (two-tailed).}")   

njvt_party <-  stargazer(njvt_party1,njvt_party2,njvt_party3,njvt_party4,
                         dep.var.labels = c("Party Unity Score"),
                         order=c(1,6,2,3,4,5,7),
                         covariate.labels = c("Chamber Larger",
                                              "Larger $\\times$ In Maj.",
                                              "In Majority",
                                              "Majority Seat Share", "Incumbent",
                                              "Republican","Republican $\\times$ Vermont"),
                         keep.stat = c("n"),report = "vc*s",table.placement = "!ht",
                         add.lines = list(c("Fixed Effects",
                                            "\\multicolumn{2}{c}{State+Year}",
                                            "\\multicolumn{2}{c}{State+Year} \\\\ %")),
                         title = "Controlling for Individual Party Affiliation \\vspace{-0.75em}",label="partyaff",
                         notes.append = FALSE,notes.label = "", no.space=T,font.size="footnotesize",
                         omit.table.layout = c("l#"),
                         digits=3,digits.extra=3,
                         star.char=c("*","**"),star.cutoffs = c(.1,.05),
                         notes="\\parbox[t]{0.625\\textwidth}{\\footnotesize \\textit{Note}: Entries are linear regression coefficients with 
                       standard errors clustered by legislator in parentheses. 
                       Observations are at the legislator-biennium level. 
              $^{**}$p$<$0.05, $^{*}$p$<$0.10 (two-tailed).}")   

partyaff_panel <- star_panel(ilin_party,njvt_party,panel.label.fontface="bold",
                             panel.names = c("Illinois and Indiana", 
                                             "New Jersey and Vermont"),
                             same.lhs.vars = F,same.summary.stats = F)
cat(partyaff_panel, sep = '\n', file = "./Paper/appendix_materials/partyaff.tex")


##################################################################################
# Section B.6 Party Unity Scores with Alternative Thresholds
##################################################################################

# Figure B.1 differences in party unity vote levels #######################################

ilpu <- read.csv("Data/il_sample_50_75_coding.csv")
ilpu <- ilpu[!is.na(ilpu$category),]
ilpu$passage <- ifelse(is.element(ilpu$category,c(1,2,3)),1,0)
ilpu$amend_proc <- ifelse(is.element(ilpu$category,c(4,5,6)),1,0)

ilpu <- ilpu[,c("year","vote_num","page","volume","level","passage","amend_proc")]
ilpu$vote_type <- "Passage";ilpu[ilpu$amend_proc==1,"vote_type"] <- "Amendment or Procedural"
ilpu[,"Party Unity Threshold"] <- ifelse(ilpu$level==50,"Fifty Percent","Seventy-Five Percent")  

ilpu_cs <- as.data.frame(as.data.frame(table(ilpu$`Party Unity Threshold`,ilpu$vote_type)) %>% pivot_wider(names_from="Var2",values_from="Freq"))
rownames(ilpu_cs) <- ilpu_cs$Var1; ilpu_cs$Var1 <- NULL
ilpu_cs <- as.matrix(ilpu_cs)
ilpu_cs_out <- prop.test(ilpu_cs)$p.value

writeLines(as.character(round(ilpu_cs_out,2)),"./Paper/appendix_materials/chisq_p.tex")

ggplot(ilpu,aes(x=vote_type,group=`Party Unity Threshold`,colour=`Party Unity Threshold`,fill=`Party Unity Threshold`))+
  geom_bar(position=position_dodge())+
  scale_fill_grey()+scale_colour_grey()+
  theme_minimal()+
  theme(axis.title.x = element_blank(),
        legend.position = "bottom")+
  ylab("Count")

ggsave("./Paper/appendix_materials/il_pu_classifications.pdf",device="pdf",width=7,height=4,units="in")

# Figure B.2 find the mean party unity scores ######################################################### 

dat_ilin$id <- dat_ilin$ko_id
pu_levels <- rbind(dat_njvt[,c("state","id","year","partyunity","pu55","pu60","pu65","pu70","pu75","pu80","pu85","pu90")],
                   dat_ilin[,c("state","id","year","partyunity","pu55","pu60","pu65","pu70","pu75","pu80","pu85","pu90")])

pu_levels <- plyr::rename(pu_levels,replace=c("partyunity"="pu50"))
pu_levels <- pu_levels %>% pivot_longer(cols=pu50:pu90)
pu_levels$threshold <- as.numeric(gsub("pu","",fixed=T,pu_levels$name))

pu_levels <- pu_levels %>% group_by(state,threshold) %>% summarise(mean=mean(value,na.rm=T),
                                                                   median=median(value,na.rm=T),
                                                                   sd=sd(value,na.rm=T),
                                                                   prop80 =length(value[value>0.8 & !is.na(value)])/length(value[!is.na(value)]))#,

pu_levels <- pu_levels %>% pivot_longer(cols=mean:prop80)
pu_levels$name <- recode(pu_levels$name,"mean"="Mean",
                         "median"="Median",
                         "sd"="Standard Deviation",
                         "prop80"="Proportion over 0.8")

ggplot(pu_levels,aes(x=threshold,y=value,group=state,colour=state))+
  geom_point(size=2)+
  geom_line()+
  facet_wrap(~name,nrow=2,scales="free")+
  theme_bw()+
  theme(legend.position = "bottom",
        strip.background = element_rect(fill="white"))+
  xlab("Party Unity Threshold")+
  ylab("Value")+
  labs(colour="State: ")+
  scale_colour_grey()

ggsave("./Paper/appendix_materials/ceiling_effects.pdf",device="pdf",width=7,height=5,units="in")


# Figure B.3 different party unity thresholds ###################################################

ilin_threshdat <- rbind(
  summary(felm(partyunity~bigger+majority+maj_share+inc|state+mod_year|0|ko_id,data=dat_ilin))$coefficients["bigger",1:2],
  summary(felm(pu55~bigger+majority+maj_share+inc|state+mod_year|0|ko_id,data=dat_ilin))$coefficients["bigger",1:2],
  summary(felm(pu60~bigger+majority+maj_share+inc|state+mod_year|0|ko_id,data=dat_ilin))$coefficients["bigger",1:2],
  summary(felm(pu65~bigger+majority+maj_share+inc|state+mod_year|0|ko_id,data=dat_ilin))$coefficients["bigger",1:2],
  summary(felm(pu70~bigger+majority+maj_share+inc|state+mod_year|0|ko_id,data=dat_ilin))$coefficients["bigger",1:2],
  summary(felm(pu75~bigger+majority+maj_share+inc|state+mod_year|0|ko_id,data=dat_ilin))$coefficients["bigger",1:2],
  summary(felm(pu80~bigger+majority+maj_share+inc|state+mod_year|0|ko_id,data=dat_ilin))$coefficients["bigger",1:2],
  summary(felm(pu85~bigger+majority+maj_share+inc|state+mod_year|0|ko_id,data=dat_ilin))$coefficients["bigger",1:2],
  summary(felm(pu90~bigger+majority+maj_share+inc|state+mod_year|0|ko_id,data=dat_ilin))$coefficients["bigger",1:2]
)

ilin_threshdat <- as.data.frame(ilin_threshdat) %>% mutate_all(as.numeric)
ilin_threshdat$threshold <- seq(50,90,by=5)

votes_thresholds <- dat_ilin %>% distinct(year,state,.keep_all=T)
votes_thresholds <- votes_thresholds[,c("year","state",grep("tot_puv",colnames(votes_thresholds),value=T))]
votes_thresholds <- votes_thresholds %>% pivot_longer(cols=tot_puvotes:tot_puv90)
votes_thresholds[votes_thresholds$name=="tot_puvotes","name"] <- "tot_puv50"
votes_thresholds <- votes_thresholds %>% group_by(name,state) %>% summarise(votes = sum(value))
votes_thresholds$threshold <- as.integer(gsub("tot_puv","",fixed=T,votes_thresholds$name))
votes_thresholds$name <- NULL
votes_thresholds <- votes_thresholds %>% pivot_wider(names_from=state,values_from=votes)

ilin_threshdat <- left_join(ilin_threshdat,votes_thresholds)
ilin_threshdat$IL <- paste("IL:",ilin_threshdat$IL)
ilin_threshdat$IN <- paste("IN:",ilin_threshdat$IN)

ggplot(ilin_threshdat,aes(x=threshold,y=Estimate))+
  geom_hline(size=1.5,linetype=2,colour="indianred",yintercept=0)+
  geom_point(size=3)+
  geom_linerange(aes(ymin=Estimate-1.96*`Cluster s.e.`,
                     ymax=Estimate+1.96*`Cluster s.e.`),
                 size=1.5)+
  geom_text(aes(x=threshold,y=min(ilin_threshdat$Estimate-1.96*ilin_threshdat$`Cluster s.e.`)-0.04,
                label=IN),size=4)+
  geom_text(aes(x=threshold,y=min(ilin_threshdat$Estimate-1.96*ilin_threshdat$`Cluster s.e.`)-0.02,
                label=IL),size=4)+
  xlab("Threshold")+
  theme_minimal()

ggsave("./Paper/appendix_materials/ilin_thresholds.pdf",width=8,height=5)


njvt_threshdat <- rbind(
  summary(felm(partyunity~bigger+majority+maj_share+inc|state+modyear1|0|id,data=dat_njvt))$coefficients["bigger",1:2],
  summary(felm(pu55~bigger+majority+maj_share+inc|state+modyear1|0|id,data=dat_njvt))$coefficients["bigger",1:2],
  summary(felm(pu60~bigger+majority+maj_share+inc|state+modyear1|0|id,data=dat_njvt))$coefficients["bigger",1:2],
  summary(felm(pu65~bigger+majority+maj_share+inc|state+modyear1|0|id,data=dat_njvt))$coefficients["bigger",1:2],
  summary(felm(pu70~bigger+majority+maj_share+inc|state+modyear1|0|id,data=dat_njvt))$coefficients["bigger",1:2],
  summary(felm(pu75~bigger+majority+maj_share+inc|state+modyear1|0|id,data=dat_njvt))$coefficients["bigger",1:2],
  summary(felm(pu80~bigger+majority+maj_share+inc|state+modyear1|0|id,data=dat_njvt))$coefficients["bigger",1:2],
  summary(felm(pu85~bigger+majority+maj_share+inc|state+modyear1|0|id,data=dat_njvt))$coefficients["bigger",1:2],
  summary(felm(pu90~bigger+majority+maj_share+inc|state+modyear1|0|id,data=dat_njvt))$coefficients["bigger",1:2]
)

njvt_threshdat <- as.data.frame(njvt_threshdat) %>% mutate_all(as.numeric)
njvt_threshdat$threshold <- seq(50,90,by=5)

njvt_votes_thresholds <- dat_njvt %>% distinct(year,state,.keep_all=T)
njvt_votes_thresholds <- njvt_votes_thresholds[,c("year","state",grep("tot_puv",colnames(njvt_votes_thresholds),value=T))]
njvt_votes_thresholds <- njvt_votes_thresholds %>% pivot_longer(cols=tot_puvotes:tot_puv90)
njvt_votes_thresholds[njvt_votes_thresholds$name=="tot_puvotes","name"] <- "tot_puv50"
njvt_votes_thresholds <- njvt_votes_thresholds %>% group_by(name,state) %>% summarise(votes = sum(value))
njvt_votes_thresholds$threshold <- as.integer(gsub("tot_puv","",fixed=T,njvt_votes_thresholds$name))
njvt_votes_thresholds$name <- NULL
njvt_votes_thresholds <- njvt_votes_thresholds %>% pivot_wider(names_from=state,values_from=votes)

njvt_threshdat <- left_join(njvt_threshdat,njvt_votes_thresholds)
njvt_threshdat$NJ <- paste("NJ:",njvt_threshdat$NJ)
njvt_threshdat$VT <- paste("VT:",njvt_threshdat$VT)

ggplot(njvt_threshdat,aes(x=threshold,y=Estimate))+
  geom_hline(size=1.5,linetype=2,colour="indianred",yintercept=0)+
  geom_point(size=3)+
  geom_linerange(aes(ymin=Estimate-1.96*`Cluster s.e.`,
                     ymax=Estimate+1.96*`Cluster s.e.`),
                 size=1.5)+
  geom_text(aes(x=threshold,y=min(njvt_threshdat$Estimate-1.96*njvt_threshdat$`Cluster s.e.`)-0.02,
                label=NJ),size=4)+
  geom_text(aes(x=threshold,y=min(njvt_threshdat$Estimate-1.96*njvt_threshdat$`Cluster s.e.`)-0.01,
                label=VT),size=4)+
  xlab("Threshold")+
  theme_minimal()

ggsave("./Paper/appendix_materials/njvt_thresholds.pdf",width=8,height=5)  

# how many legislators do we have within-legislator variation for at different levels?

r_il <- aggregate(bigger~ko_id,data=dat_ilin[!is.na(dat_ilin$partyunity),],FUN=mean)
t_il <- aggregate(bigger~ko_id,data=dat_ilin[!is.na(dat_ilin$pu90),],FUN=mean)

cat(as.character(as.english(nrow(r_il[r_il$bigger>0 & r_il$bigger<1,]))), sep = '\n', file = "./Paper/appendix_materials/il_na_50.tex")
cat(as.character(as.english(nrow(t_il[t_il$bigger>0 & t_il$bigger<1,]))), sep = '\n', file = "./Paper/appendix_materials/il_na_90.tex")

r_vt <- aggregate(bigger~id,data=dat_njvt[!is.na(dat_njvt$partyunity) & dat_njvt$state=="VT",],FUN=mean)
t_vt <- aggregate(bigger~id,data=dat_njvt[!is.na(dat_njvt$pu90) & dat_njvt$state=="VT",],FUN=mean)

cat(as.character(as.english(nrow(r_vt[r_vt$bigger>0 & r_vt$bigger<1,]))), sep = '\n', file = "./Paper/appendix_materials/vt_na_50.tex")
cat(as.character(as.english(nrow(t_vt[t_vt$bigger>0 & t_vt$bigger<1,]))), sep = '\n', file = "./Paper/appendix_materials/vt_na_90.tex")

################################################################################  
# Section B.7 Sampling-Based Party Unity Score ################################  
################################################################################

  il_cor <- cor(dat_ilin$partyunity[dat_ilin$state=="IL"],dat_ilin$pu_partysample[dat_ilin$state=="IL"],use="complete.obs")
  in_cor <- cor(dat_ilin$partyunity[dat_ilin$state=="IN"],dat_ilin$pu_partysample[dat_ilin$state=="IN"],use="complete.obs")
  nj_cor <- cor(dat_njvt$partyunity[dat_njvt$state=="NJ"],dat_njvt$pu_partysample[dat_njvt$state=="NJ"],use="complete.obs")
  vt_cor <- cor(dat_njvt$partyunity[dat_njvt$state=="VT"],dat_njvt$pu_partysample[dat_njvt$state=="VT"],use="complete.obs")

#Table B.12: Sampling-Based Party Unity Measure: Illinois and Indiana, 1838 to 1851  
  
# Illinois + Indiana 

ilin_state_pus <- felm(pu_partysample~bigger+majority+maj_share+inc|state+mod_year|0|ko_id,data=dat_ilin)
ilin_state_int_pus <- felm(pu_partysample~bigger*majority+maj_share+inc|state+mod_year|0|ko_id,data=dat_ilin)
ilin_id_pus <- felm(pu_partysample~bigger+majority+maj_share+inc|ko_id+mod_year|0|ko_id,data=dat_ilin)
ilin_id_int_pus <- felm(pu_partysample~bigger*majority+maj_share+inc|ko_id+mod_year|0|ko_id,data=dat_ilin)

ilin_state_cont_pus <- felm(pu_partysample~lht+majority+maj_share+inc|state+mod_year|0|ko_id,data=dat_ilin)
ilin_state_int_cont_pus <- felm(pu_partysample~lht*majority+maj_share+inc|state+mod_year|0|ko_id,data=dat_ilin)
ilin_id_cont_pus <- felm(pu_partysample~lht+majority+maj_share+inc|ko_id+mod_year|0|ko_id,data=dat_ilin)
ilin_id_int_cont_pus <- felm(pu_partysample~lht*majority+maj_share+inc|ko_id+mod_year|0|ko_id,data=dat_ilin)

ilinsg_pus <-  stargazer(ilin_state_pus,ilin_state_int_pus,ilin_id_pus,ilin_id_int_pus,
                         dep.var.labels = c("Party Unity (Alternative Measure)"),
                         order=c(1,5,2,3,4),
                         covariate.labels = c("Chamber Larger",
                                              "Larger $\\times$ In Maj.",
                                              "In Majority",
                                              "Majority Seat Share", "Incumbent"),
                         keep.stat = c("n"),report = "vc*s",table.placement = "!ht",
                         add.lines = list(c("Fixed Effects","\\multicolumn{2}{c}{State + Year}",
                                            "\\multicolumn{2}{c}{Legislator + Year} \\\\ %")),
                         title = "Sampling-Based Party Unity Measure: Illinois and Indiana, 1838 to 1851 \\vspace{-0.75em}",label="ilintab_pus",
                         notes.append = FALSE,notes.label = "", no.space=T,
                         digits=3,digits.extra=3,
                         omit.table.layout = "l#",
                         star.char=c("*","**"),star.cutoffs = c(.1,.05),
                         notes="\\parbox[t]{0.75\\textwidth}{\\footnotesize \\textit{Note}: Entries are linear regression coefficients with 
                       standard errors clustered by legislator in parentheses. Observations are at the legislator-biennium level. 
              $^{**}$p$<$0.05, $^{*}$p$<$0.10 (two-tailed).}")   

ilinsg_cont_pus <-  stargazer(ilin_state_cont_pus,ilin_state_int_cont_pus,ilin_id_cont_pus,ilin_id_int_cont_pus,
                              dep.var.labels = c("Party Unity (Alternative Measure)"),
                              order=c(1,5,2,3,4),
                              covariate.labels = c("Chamber Size",
                                                   "Size $\\times$ In Maj.",
                                                   "In Majority",
                                                   "Majority Seat Share", "Incumbent"),
                              keep.stat = c("n"),report = "vc*s",table.placement = "!ht",
                              add.lines = list(c("Fixed Effects","State + Year","State + Year","Leg. + Year","Leg. + Year")),
                              title = "Sampling-Based Party Unity Measure: Illinois and Indiana, 1838 to 1851  \\vspace{-0.75em}",label="ilintab_pus",
                              notes.append = FALSE,notes.label = "", no.space=T,
                              digits=3,digits.extra=3,
                              omit.table.layout = "l#",
                              star.char=c("*","**"),star.cutoffs = c(.1,.05),
                              notes="\\parbox[t]{0.75\\textwidth}{\\footnotesize \\textit{Note}: Entries are linear regression coefficients with 
                       standard errors clustered by legislator in parentheses. Observations are at the legislator-biennium level. 
              $^{**}$p$<$0.05, $^{*}$p$<$0.10 (two-tailed).}")   

inil_panels_pus <- star_panel(ilinsg_pus,ilinsg_cont_pus,panel.label.fontface="bold",
                              panel.names = c("Binary Independent Variable", 
                                              "Continuous Independent Variable"),
                              same.summary.stats = T,
                              same.lhs.vars = F)
cat(inil_panels_pus, sep = '\n', file = "./Paper/appendix_materials/pus_ilin.tex")

# Table B.13: Sampling-Based Party Unity Measure: New Jersey and Vermont, 1957-1974

njvt2_pus <- felm(pu_partysample~bigger+majority+maj_share+inc|state+modyear1|0|id,data=dat_njvt)
njvtint2_pus <- felm(pu_partysample~bigger*majority+maj_share+inc|state+modyear1|0|id,data=dat_njvt)
njvt3_pus <- felm(pu_partysample~bigger+majority+maj_share+inc|id+modyear1|0|id,data=dat_njvt)
njvtint1_pus <- felm(pu_partysample~bigger*majority+maj_share+inc|id+modyear1|0|id,data=dat_njvt)

njvt2_cont_pus <- felm(pu_partysample~lht+majority+maj_share+inc|state+modyear1|0|id,data=dat_njvt)
njvtint2_cont_pus <- felm(pu_partysample~lht*majority+maj_share+inc|state+modyear1|0|id,data=dat_njvt)
njvt3_cont_pus <- felm(pu_partysample~lht+majority+maj_share+inc|id+modyear1|0|id,data=dat_njvt)
njvtint1_cont_pus <- felm(pu_partysample~lht*majority+maj_share+inc|id+modyear1|0|id,data=dat_njvt)

njvtsg_pus <-  stargazer(njvt2_pus,njvtint2_pus,njvt3_pus,njvtint1_pus,
                         dep.var.labels = c("Party Unity Score"),
                         order=c(1,5,2,3,4),
                         covariate.labels = c("Chamber Larger",
                                              "Larger $\\times$ In Maj.",
                                              "In Majority",
                                              "Majority Seat Share", "Incumbent"),
                         keep.stat = c("n"),report = "vc*s",table.placement = "!ht",
                         add.lines = list(c("Fixed Effects","\\multicolumn{2}{c}{State + Year}","\\multicolumn{2}{c}{Leg. + Year} \\\\ %")),
                         title = "Sampling-Based Party Unity Measure: New Jersey and Vermont, 1957-1974 \\vspace{-0.75em}",label="njvttab_pus",
                         notes.append = FALSE,notes.label = "", no.space=T,
                         #table.layout ="-ld-#-t-as-n",
                         omit.table.layout = "l#",
                         digits=3,digits.extra=3,
                         star.char=c("*","**"),star.cutoffs = c(.1,.05),
                         notes="\\parbox[t]{0.75\\textwidth}{\\footnotesize \\textit{Note}: Entries are linear regression coefficients with 
                       standard errors clustered by legislator in parentheses. Observations are at the legislator-biennium level. 
              $^{**}$p$<$0.05, $^{*}$p$<$0.10 (two-tailed).}")   

njvtsg_cont_pus <-  stargazer(njvt2_cont_pus,njvtint2_cont_pus,njvt3_cont_pus,njvtint1_cont_pus,
                              dep.var.labels = c("Party Unity Score"),
                              order=c(1,5,2,3,4),
                              covariate.labels = c("Chamber Size",
                                                   "Size $\\times$ In Maj.",
                                                   "In Majority",
                                                   "Majority Seat Share", "Incumbent"),
                              keep.stat = c("n"),report = "vc*s",table.placement = "!ht",
                              add.lines = list(c("Fixed Effects","\\multicolumn{2}{c}{State + Year}","\\multicolumn{2}{c}{Leg. + Year} \\\\ %")),
                              title = "Sampling-Based Party Unity Measure: New Jersey and Vermont, 1957-1974 \\vspace{-0.75em}",label="njvttab_pus",
                              notes.append = FALSE,notes.label = "", no.space=T,
                              #table.layout ="-ld-#-t-as-n",
                              omit.table.layout = "l#",
                              digits=3,digits.extra=3,
                              star.char=c("*","**"),star.cutoffs = c(.1,.05),
                              notes="\\parbox[t]{0.75\\textwidth}{\\footnotesize \\textit{Note}: Entries are linear regression coefficients with 
                       standard errors clustered by legislator in parentheses. Observations are at the legislator-biennium level. 
              $^{**}$p$<$0.05, $^{*}$p$<$0.10 (two-tailed).}")  

njvt_panels_pus <- star_panel(njvtsg_pus,njvtsg_cont_pus,panel.label.fontface="bold",
                              panel.names = c("Binary Independent Variable", 
                                              "Continuous Independent Variable"),
                              same.lhs.vars = F,same.summary.stats = T)
cat(njvt_panels_pus, sep = '\n', file = "./Paper/appendix_materials/pus_njvt.tex")

# Figure B.4 alternative sample size thresholds

  ilinmin <- ilin_state_pus
  ilin10 <- felm(pus10~bigger+majority+maj_share+inc|state+mod_year|0|ko_id,data=dat_ilin)
  ilin5 <- felm(pus5~bigger+majority+maj_share+inc|state+mod_year|0|ko_id,data=dat_ilin)
  ilin2 <- felm(pus2~bigger+majority+maj_share+inc|state+mod_year|0|ko_id,data=dat_ilin)

  ilin_sample_plot <- as.data.frame(rbind(summary(ilinmin)$coefficients["bigger",1:2],
                                    summary(ilin10)$coefficients["bigger",1:2],
                                    summary(ilin5)$coefficients["bigger",1:2],
                                    summary(ilin2)$coefficients["bigger",1:2]))
  ilin_sample_plot$samp <- factor(c("21 IL / 20 IN","10","5","2"),
                                  levels=c("21 IL / 20 IN","10","5","2"))
  
  ggplot(ilin_sample_plot,aes(x=samp,y=Estimate,ymin=Estimate-2*`Cluster s.e.`,ymax=Estimate+2*`Cluster s.e.`))+
    geom_hline(yintercept=0,linetype=2,colour="grey30")+
    geom_point(size=2)+
    geom_linerange(size=1.25)+
    xlab("Sample Size")+ylab("Estimate of 'Chamber Larger'")+
    theme_minimal()
  
  ggsave("./Paper/appendix_materials/ilin_sample_size.pdf",device="pdf",width=6,height=4,units="in")
  
  
  njvtmin <- njvt2_pus
  njvt10 <- felm(pus10~bigger+majority+maj_share+inc|state+modyear1|0|id,data=dat_njvt)
  njvt5 <- felm(pus5~bigger+majority+maj_share+inc|state+modyear1|0|id,data=dat_njvt)
  njvt2 <- felm(pus2~bigger+majority+maj_share+inc|state+modyear1|0|id,data=dat_njvt)
  
  njvt_sample_plot <- as.data.frame(rbind(summary(njvtmin)$coefficients["bigger",1:2],
                                          summary(njvt10)$coefficients["bigger",1:2],
                                          summary(njvt5)$coefficients["bigger",1:2],
                                          summary(njvt2)$coefficients["bigger",1:2]))
  njvt_sample_plot$samp <- factor(c("30 VT / 12 NJ","10","5","2"),
                                  levels=c("30 VT / 12 NJ","10","5","2"))
  
  ggplot(njvt_sample_plot,aes(x=samp,y=Estimate,ymin=Estimate-2*`Cluster s.e.`,ymax=Estimate+2*`Cluster s.e.`))+
    geom_hline(yintercept=0,linetype=2,colour="grey30")+
    geom_point(size=2)+
    geom_linerange(size=1.25)+
    xlab("Sample Size")+ylab("Estimate of 'Chamber Larger'")+
    theme_minimal()
  
  ggsave("./Paper/appendix_materials/njvt_sample_size.pdf",device="pdf",width=6,height=4,units="in")
  
# correlations between regular pu scores and sampling-based 
  
  pu_pus <- c(cor(dat_ilin$partyunity[dat_ilin$state=="IL"],dat_ilin$pu_partysample[dat_ilin$state=="IL"],use="complete.obs"),
              cor(dat_ilin$partyunity[dat_ilin$state=="IN"],dat_ilin$pu_partysample[dat_ilin$state=="IN"],use="complete.obs"),
              cor(dat_njvt$partyunity[dat_njvt$state=="NJ"],dat_njvt$pu_partysample[dat_njvt$state=="NJ"],use="complete.obs"),
              cor(dat_njvt$partyunity[dat_njvt$state=="VT"],dat_njvt$pu_partysample[dat_njvt$state=="VT"],use="complete.obs"))
  
  cat(as.character(format(round(min(pu_pus),2),nsmall=2)), sep = '\n', file = "./Paper/appendix_materials/pus_cors.tex")


################################################################################  
# Section B.8 Accounting for Illinois's Multiple Changes #######################  
################################################################################

# Table B.14 considering Illinois's two changes separately #################################
  
b <- felm(partyunity~bigger+majority+maj_share+inc|state+mod_year|0|ko_id,data=dat_ilin[dat_ilin$mod_year<=1846,])
b2 <- felm(partyunity~bigger*majority+maj_share+inc|state+mod_year|0|ko_id,data=dat_ilin[dat_ilin$mod_year<=1846,])
b3 <- felm(partyunity~bigger+majority+maj_share+inc|ko_id+mod_year|0|ko_id,data=dat_ilin[dat_ilin$mod_year<=1846,])
b4 <- felm(partyunity~bigger*majority+maj_share+inc|ko_id+mod_year|0|ko_id,data=dat_ilin[dat_ilin$mod_year<=1846,])
  
bcont <- felm(partyunity~lht+majority+maj_share+inc|state+mod_year|0|ko_id,data=dat_ilin[dat_ilin$mod_year<=1846,])
bcont2 <- felm(partyunity~lht*majority+maj_share+inc|state+mod_year|0|ko_id,data=dat_ilin[dat_ilin$mod_year<=1846,])
bcont3 <- felm(partyunity~lht+majority+maj_share+inc|ko_id+mod_year|0|ko_id,data=dat_ilin[dat_ilin$mod_year<=1846,])
bcont4 <- felm(partyunity~lht*majority+maj_share+inc|ko_id+mod_year|0|ko_id,data=dat_ilin[dat_ilin$mod_year<=1846,])
  
dat_ilin$after_outcome <- dat_ilin$partyunity
  
a <- felm(after_outcome~bigger+majority+maj_share+inc|state+mod_year|0|ko_id,data=dat_ilin[dat_ilin$mod_year>=1842,])
a2 <- felm(after_outcome~bigger*majority+maj_share+inc|state+mod_year|0|ko_id,data=dat_ilin[dat_ilin$mod_year>=1842,])
a3 <- felm(after_outcome~bigger+majority+maj_share+inc|ko_id+mod_year|0|ko_id,data=dat_ilin[dat_ilin$mod_year>=1842,])
a4 <- felm(after_outcome~bigger*majority+maj_share+inc|ko_id+mod_year|0|ko_id,data=dat_ilin[dat_ilin$mod_year>=1842,])
  
acont <- felm(after_outcome~lht+majority+maj_share+inc|state+mod_year|0|ko_id,data=dat_ilin[dat_ilin$mod_year>=1842,])
acont2 <- felm(after_outcome~lht*majority+maj_share+inc|state+mod_year|0|ko_id,data=dat_ilin[dat_ilin$mod_year>=1842,])
acont3 <- felm(after_outcome~lht+majority+maj_share+inc|ko_id+mod_year|0|ko_id,data=dat_ilin[dat_ilin$mod_year>=1842,])
acont4 <- felm(after_outcome~lht*majority+maj_share+inc|ko_id+mod_year|0|ko_id,data=dat_ilin[dat_ilin$mod_year>=1842,])
  
ilinsg_ba <-  stargazer(b,b2,b3,b4,
                          a,a2,a3,a4,
                          dep.var.caption = c("Party Unity Score"),
                          dep.var.labels = c("1838 to 1846","1842 to 1851"),
                          order=c(1,5,2,3,4),
                          covariate.labels = c("Chamber Larger",
                                               "Larger $\\times$ In Maj.",
                                               "In Majority",
                                               "Maj. Seat Share", "Incumbent"),
                          keep.stat = c("n"),report = "vc*s",table.placement = "!ht",
                          add.lines = list(c("Fixed Effects","\\multicolumn{2}{c}{State + Year}",
                                             "\\multicolumn{2}{c}{Legislator + Year}",
                                             "\\multicolumn{2}{c}{State + Year}",
                                             "\\multicolumn{2}{c}{Legislator + Year} \\\\ %")),
                          title = "Considering Illinois' Changes Separately \\vspace{-0.75em}",label="ilintab_ba",
                          notes.append = FALSE,notes.label = "", no.space=T,
                          digits=3,digits.extra=3,font.size="footnotesize",
                          omit.table.layout ="l#",column.sep.width = "0pt",
                          star.char=c("*","**"),star.cutoffs = c(.1,.05),
                          notes="\\parbox[t]{\\textwidth}{\\footnotesize \\textit{Note}: Entries are linear regression coefficients with
                       standard errors clustered by legislator in parentheses. Observations are at the legislator-biennium level.
              $^{**}$p$<$0.05, $^{*}$p$<$0.10 (two-tailed).}")    
  
ilinsg_ba_cont <-  stargazer(bcont,bcont2,bcont3,bcont4,
                               acont,acont2,acont3,acont4,
                               dep.var.caption = c("Party Unity Score"),
                               dep.var.labels = c("1838 to 1846","1842 to 1851"),
                               order=c(1,5,2,3,4),
                               covariate.labels = c("Chamber Size",
                                                    "Size $\\times$ In Maj.",
                                                    "In Majority",
                                                    "Maj. Seat Share", "Incumbent"),
                               keep.stat = c("n"),report = "vc*s",table.placement = "!ht",
                               add.lines = list(c("Fixed Effects","\\multicolumn{2}{c}{State + Year}",
                                                  "\\multicolumn{2}{c}{Legislator + Year}",
                                                  "\\multicolumn{2}{c}{State + Year}",
                                                  "\\multicolumn{2}{c}{Legislator + Year} \\\\ %")),
                               title = "Considering Illinois' Changes Separately \\vspace{-0.75em}",label="ilintab_ba",
                               notes.append = FALSE,notes.label = "", no.space=T,
                               digits=3,digits.extra=3,font.size="footnotesize",
                               omit.table.layout ="l#",column.sep.width = "0pt",
                               star.char=c("*","**"),star.cutoffs = c(.1,.05),
                               notes="\\parbox[t]{\\textwidth}{\\footnotesize \\textit{Note}: Entries are linear regression coefficients with
                       standard errors clustered by legislator in parentheses. Observations are at the legislator-biennium level.
              $^{**}$p$<$0.05, $^{*}$p$<$0.10 (two-tailed).}")  
  
ilinsg_ba_panel <- star_panel(ilinsg_ba,ilinsg_ba_cont,panel.label.fontface="bold",
                                panel.names = c("Binary Independent Variable",
                                                "Continuous Independent Variable"),
                                same.lhs.vars = F,same.summary.stats = T)
  
cat(ilinsg_ba_panel, sep = '\n', file = "./Paper/appendix_materials/ilintab_ba.tex")  


